﻿using System.ComponentModel;
using Rong.EasyExcel.Attributes;
using Simple.Services.System.Office.Model;

namespace Simple.Services.System.Log.Models
{
    [HeaderStyle(ColumnAutoSize = true)] // 自动列宽
    public class LoginLogExportModel : ModelBase
    {
        /// <summary>
        /// 操作人
        /// </summary>
        [Display(Name = "操作人")]
        public string? Account { get; set; }

        /// <summary>
        /// 登录来源
        /// </summary>
        /// 0:网页，1:小程序
        [IgnoreColumn]
        public int? LoginSource { get; set; }

        [Display(Name = "操作来源")]
        public string? LoginSourceName 
        {
            get
            {
                if (LoginSource.HasValue)
                {
                    return LoginSource == 0 ? "Web" : "小程序";
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// 登录类型
        /// </summary>
        /// 0:登录，1:登出
        [IgnoreColumn]
        [Display(Name = "操作类型")]
        public int? LoginType { get; set; }

        [Display(Name = "操作类型")]
        public string? LoginTypeName
        {
            get
            {
                if (LoginType.HasValue)
                {
                    return LoginSource == 0 ? "登录" : "登出";
                }
                return string.Empty;
            }
        }
        /// <summary>
        /// 浏览器
        /// </summary>
        [IgnoreColumn]
        public string? Browser { get; set; }

        /// <summary>
        /// 操作系统
        /// </summary>
        [IgnoreColumn]
        public string? OperatingSystem { get; set; }

        /// <summary>
        /// IP
        /// </summary>
        [Display(Name = "IP")]
        public string? Ip { get; set; }

        /// <summary>
        /// IP所在地区
        /// </summary>
        [Display(Name = "地区")]
        public string? IpArea { get; set; }

        /// <summary>
        /// 是否执行成功
        /// </summary>
        [IgnoreColumn]
        public bool IsSuccess { get; set; }

        [Display(Name = "是否执行成功")]
        public string? IsSuccessName => IsSuccess ? "是" : "否";
        /// <summary>
        /// 返回结果
        /// </summary>
        [IgnoreColumn]
        public string? Result { get; set; }

        [Display(Name = "操作结果")]
        [HeaderStyle(ColumnSize = 60)] // 在类上设置了自动列宽，在此处固定列宽
        public string? OperatingResult
        {
            get
            {
                if(IsSuccess)
                    return "成功";
                return Result;
            }
        }

        /// <summary>
        /// 操作时间
        /// </summary>
        [Display(Name = "操作时间")]
        [DataStyle(DataFormat = "yyyy-mm-dd hh:mm:ss")]
        public DateTime OperatingTime { get; set; }

        /// <summary>
        /// 测试
        /// </summary>
        [IgnoreColumn]
        [Display(Name = "测试小数")] 
        [DataStyle(DataFormat = "#,##0.00_ ")] // 列的格式
        [ColumnStats((int)FunctionEnum.Sum)] // 列合计
        [DefaultValue(typeof(decimal), "0")] // 默认值
        public decimal? Number { get; set; }

        public override void ConfigureMapper(Profile profile)
        {
            profile.CreateMap<SysLoginLogs, LoginLogExportModel>()
                 .ForMember(d => d.OperatingTime, options => options.MapFrom(s => s.OperatingTime.LocalDateTime));
        }
    }

    /// <summary>
    /// 测试导入
    /// </summary>
    public class LoginLogImportTestModel : ImportModel
    {
        [Display(Name = "操作人")]
        [Required(ErrorMessage = "{0}不能为空")]
        public virtual string? Name { get; set; }

        [Display(Name = "手机号")]
        [Required(ErrorMessage = "{0}不能为空")]
        [RegularExpression(@"^1[3456789]\d{9}$", ErrorMessage = "{0}格式错误")]
        public virtual string? Phone { get; set; }

        [Display(Name = "年龄")]
        [Required(ErrorMessage = "{0}不能为空")]
        [Range(10, 200, ErrorMessage = "{0}区间为{1}~{2}")]
        public virtual int Age { get; set; }
    }
}
